home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / OutOfPhase1.01Source / OutOfPhase Folder / LFOGenerator.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  43.2 KB  |  1,449 lines  |  [TEXT/KAHL]

  1. /* LFOGenerator.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "LFOGenerator.h"
  31. #include "Memory.h"
  32. #include "EnvelopeState.h"
  33. #include "LFOSpecifier.h"
  34. #include "LFOListSpecifier.h"
  35. #include "FloatingPoint.h"
  36. #include "RandomNumbers.h"
  37. #include "Frequency.h"
  38. #include "Multisampler.h"
  39. #include "64BitMath.h"
  40. #include "SampleConsts.h"
  41.  
  42.  
  43. #define TWOPI ((float)6.28318530717958648)
  44.  
  45.  
  46. static long                                RandomSeed = PARKANDMILLERMINIMUM;
  47.  
  48.  
  49. typedef struct LFOOneStateRec
  50.     {
  51.         /* pointer to the LFO generating function */
  52.         float                                        (*GenFunction)(struct LFOOneStateRec* State, float Phase,
  53.                                                             float OriginalValue, float Amplitude);
  54.  
  55.         /* phase index counter */
  56.         FastFixedType                        CurrentPhase;
  57.  
  58.         /* the envelope controlling the amplitude. */
  59.         EvalEnvelopeRec*                LFOAmplitudeEnvelope;
  60.         /* the envelope controlling the frequency.  the value from here is periods per second */
  61.         EvalEnvelopeRec*                LFOFrequencyEnvelope;
  62.  
  63.         /* if this is True, then modulation is linear, otherwise modulation is */
  64.         /* exponential */
  65.         LFOArithSelect                    ModulationMode;
  66.  
  67.         /* what kind of operator are we using */
  68.         LFOOscTypes                            Operator;
  69.  
  70.         /* source information for the wave table */
  71.         MultiSampleRec*                    WaveTableSourceSelector;
  72.  
  73.         /* this is true if the wave table was defined */
  74.         MyBoolean                                WaveTableWasDefined;
  75.         /* number of frames per table */
  76.         long                                        FramesPerTable;
  77.         /* number of tables */
  78.         long                                        NumberOfTables;
  79.         /* raw wave table data array */
  80.         void**                                    WaveTableMatrix;
  81.         /* number of bits in the data for the table */
  82.         NumBitsType                            TableNumBits;
  83.         /* envelope controlling wave table index */
  84.         EvalEnvelopeRec*                WaveTableIndexEnvelope;
  85.  
  86.         /* modulation method */
  87.         LFOModulationTypes            ModulationMethod;
  88.  
  89.         /* link to the next one */
  90.         struct LFOOneStateRec*    Next;
  91.     } LFOOneStateRec;
  92.  
  93.  
  94. struct LFOGenRec
  95.     {
  96.         /* list of single LFO entries, which we sum up. */
  97.         LFOOneStateRec*                    LFOList;
  98.  
  99.         /* this is the correction factor from LFOFrequencyEnvelope which converts */
  100.         /* it from periods per second into an interval we can add to CurrentPhase */
  101.         FastFixedType                        CorrectionFactor;
  102.  
  103.         /* number of envelope clock pulses per second */
  104.         float                                        EnvelopeTicksPerSecond;
  105.  
  106.         /* link for caching records */
  107.         LFOGenRec*                            GarbageLink;
  108.     };
  109.  
  110.  
  111. /* prototypes */
  112. static float                        AddConst(LFOOneStateRec* State, float Phase,
  113.                                                     float OriginalValue, float Amplitude);
  114. static float                        AddSignSine(LFOOneStateRec* State, float Phase,
  115.                                                     float OriginalValue, float Amplitude);
  116. static float                        AddPosSine(LFOOneStateRec* State, float Phase,
  117.                                                     float OriginalValue, float Amplitude);
  118. static float                        AddSignTriangle(LFOOneStateRec* State, float Phase,
  119.                                                     float OriginalValue, float Amplitude);
  120. static float                        AddPosTriangle(LFOOneStateRec* State, float Phase,
  121.                                                     float OriginalValue, float Amplitude);
  122. static float                        AddSignSquare(LFOOneStateRec* State, float Phase,
  123.                                                     float OriginalValue, float Amplitude);
  124. static float                        AddPosSquare(LFOOneStateRec* State, float Phase,
  125.                                                     float OriginalValue, float Amplitude);
  126. static float                        AddSignRamp(LFOOneStateRec* State, float Phase,
  127.                                                     float OriginalValue, float Amplitude);
  128. static float                        AddPosRamp(LFOOneStateRec* State, float Phase,
  129.                                                     float OriginalValue, float Amplitude);
  130. static float                        AddSignFuzz(LFOOneStateRec* State, float Phase,
  131.                                                     float OriginalValue, float Amplitude);
  132. static float                        AddPosFuzz(LFOOneStateRec* State, float Phase,
  133.                                                     float OriginalValue, float Amplitude);
  134. static float                        AddWaveTable(LFOOneStateRec* State, float Phase,
  135.                                                     float OriginalValue, float Amplitude);
  136. static float                        MultConst(LFOOneStateRec* State, float Phase,
  137.                                                     float OriginalValue, float Amplitude);
  138. static float                        MultSignSine(LFOOneStateRec* State, float Phase,
  139.                                                     float OriginalValue, float Amplitude);
  140. static float                        MultPosSine(LFOOneStateRec* State, float Phase,
  141.                                                     float OriginalValue, float Amplitude);
  142. static float                        MultSignTriangle(LFOOneStateRec* State, float Phase,
  143.                                                     float OriginalValue, float Amplitude);
  144. static float                        MultPosTriangle(LFOOneStateRec* State, float Phase,
  145.                                                     float OriginalValue, float Amplitude);
  146. static float                        MultSignSquare(LFOOneStateRec* State, float Phase,
  147.                                                     float OriginalValue, float Amplitude);
  148. static float                        MultPosSquare(LFOOneStateRec* State, float Phase,
  149.                                                     float OriginalValue, float Amplitude);
  150. static float                        MultSignRamp(LFOOneStateRec* State, float Phase,
  151.                                                     float OriginalValue, float Amplitude);
  152. static float                        MultPosRamp(LFOOneStateRec* State, float Phase,
  153.                                                     float OriginalValue, float Amplitude);
  154. static float                        MultSignFuzz(LFOOneStateRec* State, float Phase,
  155.                                                     float OriginalValue, float Amplitude);
  156. static float                        MultPosFuzz(LFOOneStateRec* State, float Phase,
  157.                                                     float OriginalValue, float Amplitude);
  158. static float                        MultWaveTable(LFOOneStateRec* State, float Phase,
  159.                                                     float OriginalValue, float Amplitude);
  160. static float                        InvMultConst(LFOOneStateRec* State, float Phase,
  161.                                                     float OriginalValue, float Amplitude);
  162. static float                        InvMultSignSine(LFOOneStateRec* State, float Phase,
  163.                                                     float OriginalValue, float Amplitude);
  164. static float                        InvMultPosSine(LFOOneStateRec* State, float Phase,
  165.                                                     float OriginalValue, float Amplitude);
  166. static float                        InvMultSignTriangle(LFOOneStateRec* State, float Phase,
  167.                                                     float OriginalValue, float Amplitude);
  168. static float                        InvMultPosTriangle(LFOOneStateRec* State, float Phase,
  169.                                                     float OriginalValue, float Amplitude);
  170. static float                        InvMultSignSquare(LFOOneStateRec* State, float Phase,
  171.                                                     float OriginalValue, float Amplitude);
  172. static float                        InvMultPosSquare(LFOOneStateRec* State, float Phase,
  173.                                                     float OriginalValue, float Amplitude);
  174. static float                        InvMultSignRamp(LFOOneStateRec* State, float Phase,
  175.                                                     float OriginalValue, float Amplitude);
  176. static float                        InvMultPosRamp(LFOOneStateRec* State, float Phase,
  177.                                                     float OriginalValue, float Amplitude);
  178. static float                        InvMultSignFuzz(LFOOneStateRec* State, float Phase,
  179.                                                     float OriginalValue, float Amplitude);
  180. static float                        InvMultPosFuzz(LFOOneStateRec* State, float Phase,
  181.                                                     float OriginalValue, float Amplitude);
  182. static float                        InvMultWaveTable(LFOOneStateRec* State, float Phase,
  183.                                                     float OriginalValue, float Amplitude);
  184.  
  185.  
  186. static LFOGenRec*                    LFOGenFreeList = NIL;
  187. static LFOOneStateRec*        LFOOneStateFreeList = NIL;
  188.  
  189.  
  190. /* flush cached LFO generator records */
  191. void                                FlushLFOGeneratorRecords(void)
  192.     {
  193.         while (LFOGenFreeList != NIL)
  194.             {
  195.                 LFOGenRec*                Temp;
  196.  
  197.                 Temp = LFOGenFreeList;
  198.                 LFOGenFreeList = LFOGenFreeList->GarbageLink;
  199.                 ReleasePtr((char*)Temp);
  200.             }
  201.  
  202.         while (LFOOneStateFreeList != NIL)
  203.             {
  204.                 LFOOneStateRec*        Temp;
  205.  
  206.                 Temp = LFOOneStateFreeList;
  207.                 LFOOneStateFreeList = LFOOneStateFreeList->Next;
  208.                 ReleasePtr((char*)Temp);
  209.             }
  210.     }
  211.  
  212.  
  213. #if DEBUG
  214. static void                    ValidateLFOGen(LFOGenRec* LFOGen)
  215.     {
  216.         LFOGenRec*                Scan;
  217.  
  218.         Scan = LFOGenFreeList;
  219.         while (Scan != NIL)
  220.             {
  221.                 if (Scan == LFOGen)
  222.                     {
  223.                         PRERR(ForceAbort,"ValidateLFOGen:  it's on the free list");
  224.                     }
  225.                 Scan = Scan->GarbageLink;
  226.             }
  227.     }
  228. #else
  229. #define ValidateLFOGen(x) ((void)0)
  230. #endif
  231.  
  232.  
  233. #if DEBUG
  234. static void                    ValidateLFOOneState(LFOOneStateRec* OneState)
  235.     {
  236.         LFOOneStateRec*        Scan;
  237.  
  238.         Scan = LFOOneStateFreeList;
  239.         while (Scan != NIL)
  240.             {
  241.                 if (Scan == OneState)
  242.                     {
  243.                         PRERR(ForceAbort,"ValidateLFOOneState:  it's on the free list");
  244.                     }
  245.                 Scan = Scan->Next;
  246.             }
  247.     }
  248. #else
  249. #define ValidateLFOOneState(x) ((void)0)
  250. #endif
  251.  
  252.  
  253. /* create a new LFO generator based on a list of specifications */
  254. /* it returns the largest pre-origin time for all of the envelopes it contains */
  255. /* AmplitudeScaling and FrequencyScaling are provided primarily for frequency */
  256. /* LFO control; other LFOs are controlled through the accent parameters, and */
  257. /* should supply 1 (no scaling) for these parameters. */
  258. LFOGenRec*                    NewLFOGenerator(struct LFOListSpecRec* LFOListSpec,
  259.                                             long* MaxPreOriginTime, float Accent1, float Accent2,
  260.                                             float Accent3, float Accent4, float FrequencyHertz,
  261.                                             float HurryUp, float TicksPerSecond, float AmplitudeScaling,
  262.                                             float FrequencyScaling, LFOArithSelect ModulationMode,
  263.                                             float FreqForMultisampling)
  264.     {
  265.         LFOGenRec*                LFOGen;
  266.         long                            ListLimit;
  267.         long                            ListScan;
  268.         LFOOneStateRec*        ListTail;
  269.         long                            MaxPreOrigin;
  270.  
  271.         CheckPtrExistence(LFOListSpec);
  272.         if (LFOGenFreeList != NIL)
  273.             {
  274.                 LFOGen = LFOGenFreeList;
  275.                 LFOGenFreeList = LFOGenFreeList->GarbageLink;
  276.             }
  277.          else
  278.             {
  279.                 LFOGen = (LFOGenRec*)AllocPtrCanFail(sizeof(LFOGenRec),"LFOGenRec");
  280.                 if (LFOGen == NIL)
  281.                     {
  282.                         return NIL;
  283.                     }
  284.             }
  285.  
  286.         /* calculate correction factor.  when this factor is multiplied by some */
  287.         /* thing in periods/second, it converts it to periods/update */
  288.         LFOGen->CorrectionFactor = Double2FastFixed(1 / TicksPerSecond);
  289.  
  290.         /* remember the envelope rate */
  291.         LFOGen->EnvelopeTicksPerSecond = TicksPerSecond;
  292.  
  293.         /* build the list of thingers */
  294.         LFOGen->LFOList = NIL;
  295.         ListTail = NIL;
  296.         ListLimit = LFOListSpecGetNumElements(LFOListSpec);
  297.         MaxPreOrigin = 0;
  298.         for (ListScan = 0; ListScan < ListLimit; ListScan += 1)
  299.             {
  300.                 LFOSpecRec*                OneLFOSpec;
  301.                 LFOOneStateRec*        ListNode;
  302.                 long                            PreOriginTime;
  303.  
  304.                 /* allocate an entry */
  305.                 if (LFOOneStateFreeList != NIL)
  306.                     {
  307.                         ListNode = LFOOneStateFreeList;
  308.                         LFOOneStateFreeList = LFOOneStateFreeList->Next;
  309.                     }
  310.                  else
  311.                     {
  312.                         ListNode = (LFOOneStateRec*)AllocPtrCanFail(sizeof(LFOOneStateRec),
  313.                             "LFOOneStateRec");
  314.                         if (ListNode == NIL)
  315.                             {
  316.                              FailurePoint1:
  317.                                 while (LFOGen->LFOList != NIL)
  318.                                     {
  319.                                         ListNode = LFOGen->LFOList;
  320.                                         LFOGen->LFOList = LFOGen->LFOList->Next;
  321.                                         DisposeEnvelopeStateRecord(ListNode->LFOAmplitudeEnvelope);
  322.                                         DisposeEnvelopeStateRecord(ListNode->LFOFrequencyEnvelope);
  323.                                         ListNode->Next = LFOOneStateFreeList;
  324.                                         LFOOneStateFreeList = ListNode;
  325.                                     }
  326.                                 LFOGen->GarbageLink = LFOGenFreeList;
  327.                                 LFOGenFreeList = LFOGen;
  328.                                 return NIL;
  329.                             }
  330.                     }
  331.                 /* initialize phase index */
  332.                 ListNode->CurrentPhase = 0;
  333.                 /* get the data to put in it */
  334.                 OneLFOSpec = LFOListSpecGetLFOSpec(LFOListSpec,ListScan);
  335.                 /* Add frequency envelope generator */
  336.                 ListNode->LFOFrequencyEnvelope = NewEnvelopeStateRecord(
  337.                     GetLFOSpecFrequencyEnvelope(OneLFOSpec),Accent1,Accent2,Accent3,Accent4,
  338.                     FrequencyHertz,FrequencyScaling,HurryUp,TicksPerSecond,&PreOriginTime);
  339.                 if (ListNode->LFOFrequencyEnvelope == NIL)
  340.                     {
  341.                      FailurePoint2:
  342.                         ListNode->Next = LFOOneStateFreeList;
  343.                         LFOOneStateFreeList = ListNode;
  344.                         goto FailurePoint1;
  345.                     }
  346.                 if (PreOriginTime > MaxPreOrigin)
  347.                     {
  348.                         MaxPreOrigin = PreOriginTime;
  349.                     }
  350.                 /* determine what mode to use and calculate amplitude */
  351.                 switch (ModulationMode)
  352.                     {
  353.                         default:
  354.                             EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation mode"));
  355.                             break;
  356.                         case eLFOArithAdditive:
  357.                             ListNode->ModulationMode = eLFOArithAdditive;
  358.                             break;
  359.                         case eLFOArithGeometric:
  360.                             ListNode->ModulationMode = eLFOArithGeometric;
  361.                             break;
  362.                         case eLFOArithDefault:
  363.                             switch (LFOSpecGetAddingMode(OneLFOSpec))
  364.                                 {
  365.                                     default:
  366.                                         EXECUTE(PRERR(ForceAbort,
  367.                                             "NewLFOGenerator:  bad value from LFOSpecGetAddingMode"));
  368.                                         break;
  369.                                     case eLFOArithmetic:
  370.                                         ListNode->ModulationMode = eLFOArithmetic;
  371.                                         break;
  372.                                     case eLFOGeometric:
  373.                                         ListNode->ModulationMode = eLFOGeometric;
  374.                                         break;
  375.                                 }
  376.                             break;
  377.                     }
  378.                 /* add the amplitude envelope generator */
  379.                 ListNode->LFOAmplitudeEnvelope = NewEnvelopeStateRecord(
  380.                     GetLFOSpecAmplitudeEnvelope(OneLFOSpec),Accent1,Accent2,Accent3,Accent4,
  381.                     FrequencyHertz,AmplitudeScaling,HurryUp,TicksPerSecond,&PreOriginTime);
  382.                 if (ListNode->LFOAmplitudeEnvelope == NIL)
  383.                     {
  384.                      FailurePoint3:
  385.                         DisposeEnvelopeStateRecord(ListNode->LFOFrequencyEnvelope);
  386.                         goto FailurePoint2;
  387.                     }
  388.                 if (PreOriginTime > MaxPreOrigin)
  389.                     {
  390.                         MaxPreOrigin = PreOriginTime;
  391.                     }
  392.                 /* determine what function to use */
  393.                 ListNode->Operator = LFOSpecGetOscillatorType(OneLFOSpec);
  394.                 ListNode->ModulationMethod = LFOSpecGetModulationMode(OneLFOSpec);
  395.                 switch (ListNode->Operator)
  396.                     {
  397.                         default:
  398.                             EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad oscillator type"));
  399.                             break;
  400.                         case eLFOConstant1:
  401.                             switch (ListNode->ModulationMethod)
  402.                                 {
  403.                                     default:
  404.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  405.                                         break;
  406.                                     case eLFOAdditive:
  407.                                         ListNode->GenFunction = &AddConst;
  408.                                         break;
  409.                                     case eLFOMultiplicative:
  410.                                         ListNode->GenFunction = &MultConst;
  411.                                         break;
  412.                                     case eLFOInverseMultiplicative:
  413.                                         ListNode->GenFunction = &InvMultConst;
  414.                                         break;
  415.                                 }
  416.                             break;
  417.                         case eLFOSignedSine:
  418.                             switch (ListNode->ModulationMethod)
  419.                                 {
  420.                                     default:
  421.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  422.                                         break;
  423.                                     case eLFOAdditive:
  424.                                         ListNode->GenFunction = &AddSignSine;
  425.                                         break;
  426.                                     case eLFOMultiplicative:
  427.                                         ListNode->GenFunction = &MultSignSine;
  428.                                         break;
  429.                                     case eLFOInverseMultiplicative:
  430.                                         ListNode->GenFunction = &InvMultSignSine;
  431.                                         break;
  432.                                 }
  433.                             break;
  434.                         case eLFOPositiveSine:
  435.                             switch (ListNode->ModulationMethod)
  436.                                 {
  437.                                     default:
  438.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  439.                                         break;
  440.                                     case eLFOAdditive:
  441.                                         ListNode->GenFunction = &AddPosSine;
  442.                                         break;
  443.                                     case eLFOMultiplicative:
  444.                                         ListNode->GenFunction = &MultPosSine;
  445.                                         break;
  446.                                     case eLFOInverseMultiplicative:
  447.                                         ListNode->GenFunction = &InvMultPosSine;
  448.                                         break;
  449.                                 }
  450.                             break;
  451.                         case eLFOSignedTriangle:
  452.                             switch (ListNode->ModulationMethod)
  453.                                 {
  454.                                     default:
  455.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  456.                                         break;
  457.                                     case eLFOAdditive:
  458.                                         ListNode->GenFunction = &AddSignTriangle;
  459.                                         break;
  460.                                     case eLFOMultiplicative:
  461.                                         ListNode->GenFunction = &MultSignTriangle;
  462.                                         break;
  463.                                     case eLFOInverseMultiplicative:
  464.                                         ListNode->GenFunction = &InvMultSignTriangle;
  465.                                         break;
  466.                                 }
  467.                             break;
  468.                         case eLFOPositiveTriangle:
  469.                             switch (ListNode->ModulationMethod)
  470.                                 {
  471.                                     default:
  472.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  473.                                         break;
  474.                                     case eLFOAdditive:
  475.                                         ListNode->GenFunction = &AddPosTriangle;
  476.                                         break;
  477.                                     case eLFOMultiplicative:
  478.                                         ListNode->GenFunction = &MultPosTriangle;
  479.                                         break;
  480.                                     case eLFOInverseMultiplicative:
  481.                                         ListNode->GenFunction = &InvMultPosTriangle;
  482.                                         break;
  483.                                 }
  484.                             break;
  485.                         case eLFOSignedSquare:
  486.                             switch (ListNode->ModulationMethod)
  487.                                 {
  488.                                     default:
  489.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  490.                                         break;
  491.                                     case eLFOAdditive:
  492.                                         ListNode->GenFunction = &AddSignSquare;
  493.                                         break;
  494.                                     case eLFOMultiplicative:
  495.                                         ListNode->GenFunction = &MultSignSquare;
  496.                                         break;
  497.                                     case eLFOInverseMultiplicative:
  498.                                         ListNode->GenFunction = &InvMultSignSquare;
  499.                                         break;
  500.                                 }
  501.                             break;
  502.                         case eLFOPositiveSquare:
  503.                             switch (ListNode->ModulationMethod)
  504.                                 {
  505.                                     default:
  506.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  507.                                         break;
  508.                                     case eLFOAdditive:
  509.                                         ListNode->GenFunction = &AddPosSquare;
  510.                                         break;
  511.                                     case eLFOMultiplicative:
  512.                                         ListNode->GenFunction = &MultPosSquare;
  513.                                         break;
  514.                                     case eLFOInverseMultiplicative:
  515.                                         ListNode->GenFunction = &InvMultPosSquare;
  516.                                         break;
  517.                                 }
  518.                             break;
  519.                         case eLFOSignedRamp:
  520.                             switch (ListNode->ModulationMethod)
  521.                                 {
  522.                                     default:
  523.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  524.                                         break;
  525.                                     case eLFOAdditive:
  526.                                         ListNode->GenFunction = &AddSignRamp;
  527.                                         break;
  528.                                     case eLFOMultiplicative:
  529.                                         ListNode->GenFunction = &MultSignRamp;
  530.                                         break;
  531.                                     case eLFOInverseMultiplicative:
  532.                                         ListNode->GenFunction = &InvMultSignRamp;
  533.                                         break;
  534.                                 }
  535.                             break;
  536.                         case eLFOPositiveRamp:
  537.                             switch (ListNode->ModulationMethod)
  538.                                 {
  539.                                     default:
  540.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  541.                                         break;
  542.                                     case eLFOAdditive:
  543.                                         ListNode->GenFunction = &AddPosRamp;
  544.                                         break;
  545.                                     case eLFOMultiplicative:
  546.                                         ListNode->GenFunction = &MultPosRamp;
  547.                                         break;
  548.                                     case eLFOInverseMultiplicative:
  549.                                         ListNode->GenFunction = &InvMultPosRamp;
  550.                                         break;
  551.                                 }
  552.                             break;
  553.                         case eLFOSignedLinearFuzz:
  554.                             switch (ListNode->ModulationMethod)
  555.                                 {
  556.                                     default:
  557.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  558.                                         break;
  559.                                     case eLFOAdditive:
  560.                                         ListNode->GenFunction = &AddSignFuzz;
  561.                                         break;
  562.                                     case eLFOMultiplicative:
  563.                                         ListNode->GenFunction = &MultSignFuzz;
  564.                                         break;
  565.                                     case eLFOInverseMultiplicative:
  566.                                         ListNode->GenFunction = &InvMultSignFuzz;
  567.                                         break;
  568.                                 }
  569.                             break;
  570.                         case eLFOPositiveLinearFuzz:
  571.                             switch (ListNode->ModulationMethod)
  572.                                 {
  573.                                     default:
  574.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  575.                                         break;
  576.                                     case eLFOAdditive:
  577.                                         ListNode->GenFunction = &AddPosFuzz;
  578.                                         break;
  579.                                     case eLFOMultiplicative:
  580.                                         ListNode->GenFunction = &MultPosFuzz;
  581.                                         break;
  582.                                     case eLFOInverseMultiplicative:
  583.                                         ListNode->GenFunction = &InvMultPosFuzz;
  584.                                         break;
  585.                                 }
  586.                             break;
  587.                         case eLFOWaveTable:
  588.                             switch (ListNode->ModulationMethod)
  589.                                 {
  590.                                     default:
  591.                                         EXECUTE(PRERR(ForceAbort,"NewLFOGenerator:  bad modulation type"));
  592.                                         break;
  593.                                     case eLFOAdditive:
  594.                                         ListNode->GenFunction = &AddWaveTable;
  595.                                         break;
  596.                                     case eLFOMultiplicative:
  597.                                         ListNode->GenFunction = &MultWaveTable;
  598.                                         break;
  599.                                     case eLFOInverseMultiplicative:
  600.                                         ListNode->GenFunction = &InvMultWaveTable;
  601.                                         break;
  602.                                 }
  603.                             ListNode->WaveTableSourceSelector = NewMultisampleWaveTable(
  604.                                 GetLFOSpecSampleSelector(OneLFOSpec));
  605.                             if (ListNode->WaveTableSourceSelector == NIL)
  606.                                 {
  607.                                  FailurePoint4:
  608.                                     if (ListNode->Operator == eLFOWaveTable)
  609.                                         {
  610.                                             DisposeEnvelopeStateRecord(ListNode->LFOAmplitudeEnvelope);
  611.                                         }
  612.                                     goto FailurePoint3;
  613.                                 }
  614.                             ListNode->WaveTableWasDefined = GetMultisampleReferenceWaveTable(
  615.                                 ListNode->WaveTableSourceSelector,FreqForMultisampling,
  616.                                 &(ListNode->WaveTableMatrix),&(ListNode->FramesPerTable),
  617.                                 &(ListNode->NumberOfTables),&(ListNode->TableNumBits));
  618.                             ListNode->WaveTableIndexEnvelope = NewEnvelopeStateRecord(
  619.                                 GetLFOSpecWaveTableIndexEnvelope(OneLFOSpec),Accent1,Accent2,Accent3,
  620.                                 Accent4,FrequencyHertz,AmplitudeScaling,HurryUp,TicksPerSecond,
  621.                                 &PreOriginTime);
  622.                             if (ListNode->WaveTableIndexEnvelope == NIL)
  623.                                 {
  624.                                  FailurePoint5:
  625.                                     if (ListNode->Operator == eLFOWaveTable)
  626.                                         {
  627.                                             DisposeMultisample(ListNode->WaveTableSourceSelector);
  628.                                         }
  629.                                     goto FailurePoint4;
  630.                                 }
  631.                             if (PreOriginTime > MaxPreOrigin)
  632.                                 {
  633.                                     MaxPreOrigin = PreOriginTime;
  634.                                 }
  635.                             break;
  636.                     }
  637.                 /* link it in */
  638.                 ListNode->Next = NIL;
  639.                 if (ListTail != NIL)
  640.                     {
  641.                         ListTail->Next = ListNode;
  642.                     }
  643.                  else
  644.                     {
  645.                         LFOGen->LFOList = ListNode;
  646.                     }
  647.                 ListTail = ListNode;
  648.             }
  649.  
  650.         *MaxPreOriginTime = MaxPreOrigin;
  651.         return LFOGen;
  652.     }
  653.  
  654.  
  655. /* fix up the origin time so that envelopes start at the proper times */
  656. void                                LFOGeneratorFixEnvelopeOrigins(LFOGenRec* LFOGen,
  657.                                             long ActualPreOriginTime)
  658.     {
  659.         LFOOneStateRec*        Scan;
  660.  
  661.         CheckPtrExistence(LFOGen);
  662.         ValidateLFOGen(LFOGen);
  663.  
  664.         Scan = LFOGen->LFOList;
  665.         while (Scan != NIL)
  666.             {
  667.                 ValidateLFOOneState(Scan);
  668.                 EnvelopeStateFixUpInitialDelay(Scan->LFOAmplitudeEnvelope,ActualPreOriginTime);
  669.                 EnvelopeStateFixUpInitialDelay(Scan->LFOFrequencyEnvelope,ActualPreOriginTime);
  670.                 if (Scan->Operator == eLFOWaveTable)
  671.                     {
  672.                         EnvelopeStateFixUpInitialDelay(Scan->WaveTableIndexEnvelope,
  673.                             ActualPreOriginTime);
  674.                     }
  675.                 Scan = Scan->Next;
  676.             }
  677.     }
  678.  
  679.  
  680. /* this function computes one LFO cycle and returns the value from the LFO generator. */
  681. /* it should be called on the envelope clock. */
  682. FastFixedType                LFOGenUpdateCycle(LFOGenRec* LFOGen, FastFixedType OriginalValue)
  683.     {
  684.         LFOOneStateRec*        Scan;
  685.  
  686.         CheckPtrExistence(LFOGen);
  687.         ValidateLFOGen(LFOGen);
  688.  
  689.         Scan = LFOGen->LFOList;
  690.         if (Scan != NIL)
  691.             {
  692.                 float                            Iterator;
  693.  
  694.                 Iterator = FastFixed2Float(OriginalValue);
  695.                 while (Scan != NIL)
  696.                     {
  697.                         /* perform the calculations */
  698.                         if (Scan->ModulationMode == eLFOArithAdditive)
  699.                             {
  700.                                 Iterator = (*Scan->GenFunction)(Scan,FastFixed2Float(Scan->CurrentPhase),
  701.                                     Iterator,FastFixed2Float(EnvelopeUpdate(Scan->LFOAmplitudeEnvelope)));
  702.                             }
  703.                          else
  704.                             {
  705.                                 MyBoolean                    Sign;
  706.  
  707.                                 Sign = (Iterator < 0);
  708.                                 if (Sign)
  709.                                     {
  710.                                         Iterator = - Iterator;
  711.                                     }
  712.                                 if (Iterator != 0)
  713.                                     {
  714.                                         /* the LOG2 is to normalize the values, so that 1/12 will */
  715.                                         /* be 1 halfstep */
  716.                                         Iterator = (float)FEXP((*Scan->GenFunction)(Scan,
  717.                                             FastFixed2Float(Scan->CurrentPhase),(float)FLN(Iterator),
  718.                                             FastFixed2Float(EnvelopeUpdate(
  719.                                             Scan->LFOAmplitudeEnvelope)) * (float)LOG2));
  720.                                     }
  721.                                 if (Sign)
  722.                                     {
  723.                                         Iterator = - Iterator;
  724.                                     }
  725.                             }
  726.                         Scan->CurrentPhase = (Scan->CurrentPhase
  727.                             + FastFixedTimesFastFixedToFastFixed(LFOGen->CorrectionFactor,
  728.                             EnvelopeUpdate(Scan->LFOFrequencyEnvelope)))
  729.                             & ((1L << FASTFIXEDPRECISION) - 1);
  730.                         /* go to next one */
  731.                         Scan = Scan->Next;
  732.                     }
  733.                 return Double2FastFixed(Iterator);
  734.             }
  735.          else
  736.             {
  737.                 return OriginalValue;
  738.             }
  739.     }
  740.  
  741.  
  742. /* pass the key-up impulse on to the envelopes contained inside */
  743. void                                LFOGeneratorKeyUpSustain1(LFOGenRec* LFOGen)
  744.     {
  745.         LFOOneStateRec*        Scan;
  746.  
  747.         CheckPtrExistence(LFOGen);
  748.         ValidateLFOGen(LFOGen);
  749.  
  750.         Scan = LFOGen->LFOList;
  751.         while (Scan != NIL)
  752.             {
  753.                 EnvelopeKeyUpSustain1(Scan->LFOAmplitudeEnvelope);
  754.                 EnvelopeKeyUpSustain1(Scan->LFOFrequencyEnvelope);
  755.                 if (Scan->Operator == eLFOWaveTable)
  756.                     {
  757.                         EnvelopeKeyUpSustain1(Scan->WaveTableIndexEnvelope);
  758.                     }
  759.                 Scan = Scan->Next;
  760.             }
  761.     }
  762.  
  763.  
  764. /* pass the key-up impulse on to the envelopes contained inside */
  765. void                                LFOGeneratorKeyUpSustain2(LFOGenRec* LFOGen)
  766.     {
  767.         LFOOneStateRec*        Scan;
  768.  
  769.         CheckPtrExistence(LFOGen);
  770.         ValidateLFOGen(LFOGen);
  771.  
  772.         Scan = LFOGen->LFOList;
  773.         while (Scan != NIL)
  774.             {
  775.                 EnvelopeKeyUpSustain2(Scan->LFOAmplitudeEnvelope);
  776.                 EnvelopeKeyUpSustain2(Scan->LFOFrequencyEnvelope);
  777.                 if (Scan->Operator == eLFOWaveTable)
  778.                     {
  779.                         EnvelopeKeyUpSustain2(Scan->WaveTableIndexEnvelope);
  780.                     }
  781.                 Scan = Scan->Next;
  782.             }
  783.     }
  784.  
  785.  
  786. /* pass the key-up impulse on to the envelopes contained inside */
  787. void                                LFOGeneratorKeyUpSustain3(LFOGenRec* LFOGen)
  788.     {
  789.         LFOOneStateRec*        Scan;
  790.  
  791.         CheckPtrExistence(LFOGen);
  792.         ValidateLFOGen(LFOGen);
  793.  
  794.         Scan = LFOGen->LFOList;
  795.         while (Scan != NIL)
  796.             {
  797.                 EnvelopeKeyUpSustain3(Scan->LFOAmplitudeEnvelope);
  798.                 EnvelopeKeyUpSustain3(Scan->LFOFrequencyEnvelope);
  799.                 if (Scan->Operator == eLFOWaveTable)
  800.                     {
  801.                         EnvelopeKeyUpSustain3(Scan->WaveTableIndexEnvelope);
  802.                     }
  803.                 Scan = Scan->Next;
  804.             }
  805.     }
  806.  
  807.  
  808. /* retrigger envelopes from the origin point */
  809. void                                LFOGeneratorRetriggerFromOrigin(LFOGenRec* LFOGen, float Accent1,
  810.                                             float Accent2, float Accent3, float Accent4, float FrequencyHertz,
  811.                                             float HurryUp, float TicksPerSecond, float AmplitudeScaling,
  812.                                             float FrequencyScaling, MyBoolean ActuallyRetrigger)
  813.     {
  814.         LFOOneStateRec*        Scan;
  815.  
  816.         CheckPtrExistence(LFOGen);
  817.         ValidateLFOGen(LFOGen);
  818.  
  819.         Scan = LFOGen->LFOList;
  820.         while (Scan != NIL)
  821.             {
  822.                 EnvelopeRetriggerFromOrigin(Scan->LFOAmplitudeEnvelope,Accent1,Accent2,
  823.                     Accent3,Accent4,FrequencyHertz,AmplitudeScaling,HurryUp,
  824.                     LFOGen->EnvelopeTicksPerSecond,ActuallyRetrigger);
  825.                 EnvelopeRetriggerFromOrigin(Scan->LFOFrequencyEnvelope,Accent1,Accent2,
  826.                     Accent3,Accent4,FrequencyHertz,FrequencyScaling,HurryUp,
  827.                     LFOGen->EnvelopeTicksPerSecond,ActuallyRetrigger);
  828.                 if (Scan->Operator == eLFOWaveTable)
  829.                     {
  830.                         EnvelopeRetriggerFromOrigin(Scan->WaveTableIndexEnvelope,Accent1,Accent2,
  831.                             Accent3,Accent4,FrequencyHertz,FrequencyScaling,HurryUp,
  832.                             LFOGen->EnvelopeTicksPerSecond,ActuallyRetrigger);
  833.                     }
  834.                 Scan = Scan->Next;
  835.             }
  836.     }
  837.  
  838.  
  839. /* dispose the LFO generator */
  840. void                                DisposeLFOGenerator(LFOGenRec* LFOGen)
  841.     {
  842.         LFOOneStateRec*        StateScan;
  843.  
  844.         CheckPtrExistence(LFOGen);
  845.         ValidateLFOGen(LFOGen);
  846.  
  847.         StateScan = LFOGen->LFOList;
  848.  
  849.         /* dispose of the master record */
  850.         LFOGen->GarbageLink = LFOGenFreeList;
  851.         LFOGenFreeList = LFOGen;
  852.  
  853.         /* dispose of each element */
  854.         while (StateScan != NIL)
  855.             {
  856.                 LFOOneStateRec*        Temp;
  857.  
  858.                 ValidateLFOOneState(StateScan);
  859.                 if (StateScan->Operator == eLFOWaveTable)
  860.                     {
  861.                         DisposeMultisample(StateScan->WaveTableSourceSelector);
  862.                         DisposeEnvelopeStateRecord(StateScan->WaveTableIndexEnvelope);
  863.                     }
  864.                 DisposeEnvelopeStateRecord(StateScan->LFOAmplitudeEnvelope);
  865.                 DisposeEnvelopeStateRecord(StateScan->LFOFrequencyEnvelope);
  866.                 Temp = StateScan;
  867.                 StateScan = StateScan->Next;
  868.                 Temp->Next = LFOOneStateFreeList;
  869.                 LFOOneStateFreeList = Temp;
  870.             }
  871.     }
  872.  
  873.  
  874. static float                        AddConst(LFOOneStateRec* State, float Phase,
  875.                                                     float OriginalValue, float Amplitude)
  876.     {
  877.         return OriginalValue + Amplitude;
  878.     }
  879.  
  880.  
  881. static float                        AddSignSine(LFOOneStateRec* State, float Phase,
  882.                                                     float OriginalValue, float Amplitude)
  883.     {
  884.         return OriginalValue + Amplitude * FSIN(Phase * TWOPI);
  885.     }
  886.  
  887.  
  888. static float                        AddPosSine(LFOOneStateRec* State, float Phase,
  889.                                                     float OriginalValue, float Amplitude)
  890.     {
  891.         return OriginalValue + Amplitude * 0.5 * (1 + FSIN(Phase * TWOPI));
  892.     }
  893.  
  894.  
  895. static float                        AddSignTriangle(LFOOneStateRec* State, float Phase,
  896.                                                     float OriginalValue, float Amplitude)
  897.     {
  898.         if (Phase < 0.25)
  899.             {
  900.             }
  901.         else if (Phase < 0.75)
  902.             {
  903.                 Phase = 0.5 - Phase;
  904.             }
  905.         else
  906.             {
  907.                 Phase = Phase - 1;
  908.             }
  909.         return OriginalValue + Phase * 4 * Amplitude;
  910.     }
  911.  
  912.  
  913. static float                        AddPosTriangle(LFOOneStateRec* State, float Phase,
  914.                                                     float OriginalValue, float Amplitude)
  915.     {
  916.         if (Phase < 0.25)
  917.             {
  918.             }
  919.         else if (Phase < 0.75)
  920.             {
  921.                 Phase = 0.5 - Phase;
  922.             }
  923.         else
  924.             {
  925.                 Phase = Phase - 1;
  926.             }
  927.         return OriginalValue + (Phase + 1) * 2 * Amplitude;
  928.     }
  929.  
  930.  
  931. static float                        AddSignSquare(LFOOneStateRec* State, float Phase,
  932.                                                     float OriginalValue, float Amplitude)
  933.     {
  934.         if (Phase < 0.5)
  935.             {
  936.                 return OriginalValue + Amplitude;
  937.             }
  938.          else
  939.             {
  940.                 return OriginalValue - Amplitude;
  941.             }
  942.     }
  943.  
  944.  
  945. static float                        AddPosSquare(LFOOneStateRec* State, float Phase,
  946.                                                     float OriginalValue, float Amplitude)
  947.     {
  948.         if (Phase < 0.5)
  949.             {
  950.                 return OriginalValue + Amplitude;
  951.             }
  952.          else
  953.             {
  954.                 return OriginalValue;
  955.             }
  956.     }
  957.  
  958.  
  959. static float                        AddSignRamp(LFOOneStateRec* State, float Phase,
  960.                                                     float OriginalValue, float Amplitude)
  961.     {
  962.         return OriginalValue + Amplitude * (2 * Phase - 1);
  963.     }
  964.  
  965.  
  966. static float                        AddPosRamp(LFOOneStateRec* State, float Phase,
  967.                                                     float OriginalValue, float Amplitude)
  968.     {
  969.         return OriginalValue + Amplitude * Phase;
  970.     }
  971.  
  972.  
  973. static float                        AddSignFuzz(LFOOneStateRec* State, float Phase,
  974.                                                     float OriginalValue, float Amplitude)
  975.     {
  976.         long                                    OldSeed;
  977.         float                                    ReturnValue;
  978.  
  979.         OldSeed = SetParkAndMillerRandomSeed(RandomSeed);
  980.         ReturnValue = ((((float)ParkAndMillerRandom() - PARKANDMILLERMINIMUM)
  981.             / (PARKANDMILLERMAXIMUM - PARKANDMILLERMINIMUM - 1) * 2) - 1) * Amplitude
  982.             + OriginalValue;
  983.         RandomSeed = SetParkAndMillerRandomSeed(OldSeed);
  984.         return ReturnValue;
  985.     }
  986.  
  987.  
  988. static float                        AddPosFuzz(LFOOneStateRec* State, float Phase,
  989.                                                     float OriginalValue, float Amplitude)
  990.     {
  991.         long                                    OldSeed;
  992.         float                                    ReturnValue;
  993.  
  994.         OldSeed = SetParkAndMillerRandomSeed(RandomSeed);
  995.         ReturnValue = (((float)ParkAndMillerRandom() - PARKANDMILLERMINIMUM)
  996.             / (PARKANDMILLERMAXIMUM - PARKANDMILLERMINIMUM - 1) * Amplitude)
  997.             + OriginalValue;
  998.         RandomSeed = SetParkAndMillerRandomSeed(OldSeed);
  999.         return ReturnValue;
  1000.     }
  1001.  
  1002.  
  1003. static float                        MultConst(LFOOneStateRec* State, float Phase,
  1004.                                                     float OriginalValue, float Amplitude)
  1005.     {
  1006.         return OriginalValue * Amplitude;
  1007.     }
  1008.  
  1009.  
  1010. static float                        MultSignSine(LFOOneStateRec* State, float Phase,
  1011.                                                     float OriginalValue, float Amplitude)
  1012.     {
  1013.         return OriginalValue * Amplitude * FSIN(Phase * TWOPI);
  1014.     }
  1015.  
  1016.  
  1017. static float                        MultPosSine(LFOOneStateRec* State, float Phase,
  1018.                                                     float OriginalValue, float Amplitude)
  1019.     {
  1020.         return OriginalValue * Amplitude * 0.5 * (1 + FSIN(Phase * TWOPI));
  1021.     }
  1022.  
  1023.  
  1024. static float                        MultSignTriangle(LFOOneStateRec* State, float Phase,
  1025.                                                     float OriginalValue, float Amplitude)
  1026.     {
  1027.         if (Phase < 0.25)
  1028.             {
  1029.             }
  1030.         else if (Phase < 0.75)
  1031.             {
  1032.                 Phase = 0.5 - Phase;
  1033.             }
  1034.         else
  1035.             {
  1036.                 Phase = Phase - 1;
  1037.             }
  1038.         return OriginalValue * Phase * 4 * Amplitude;
  1039.     }
  1040.  
  1041.  
  1042. static float                        MultPosTriangle(LFOOneStateRec* State, float Phase,
  1043.                                                     float OriginalValue, float Amplitude)
  1044.     {
  1045.         if (Phase < 0.25)
  1046.             {
  1047.             }
  1048.         else if (Phase < 0.75)
  1049.             {
  1050.                 Phase = 0.5 - Phase;
  1051.             }
  1052.         else
  1053.             {
  1054.                 Phase = Phase - 1;
  1055.             }
  1056.         return OriginalValue * (Phase + 1) * 2 * Amplitude;
  1057.     }
  1058.  
  1059.  
  1060. static float                        MultSignSquare(LFOOneStateRec* State, float Phase,
  1061.                                                     float OriginalValue, float Amplitude)
  1062.     {
  1063.         if (Phase < 0.5)
  1064.             {
  1065.                 return OriginalValue * Amplitude;
  1066.             }
  1067.          else
  1068.             {
  1069.                 return OriginalValue * - Amplitude;
  1070.             }
  1071.     }
  1072.  
  1073.  
  1074. static float                        MultPosSquare(LFOOneStateRec* State, float Phase,
  1075.                                                     float OriginalValue, float Amplitude)
  1076.     {
  1077.         if (Phase < 0.5)
  1078.             {
  1079.                 return OriginalValue * Amplitude;
  1080.             }
  1081.          else
  1082.             {
  1083.                 return 0;
  1084.             }
  1085.     }
  1086.  
  1087.  
  1088. static float                        MultSignRamp(LFOOneStateRec* State, float Phase,
  1089.                                                     float OriginalValue, float Amplitude)
  1090.     {
  1091.         return OriginalValue * Amplitude * (2 * Phase - 1);
  1092.     }
  1093.  
  1094.  
  1095. static float                        MultPosRamp(LFOOneStateRec* State, float Phase,
  1096.                                                     float OriginalValue, float Amplitude)
  1097.     {
  1098.         return OriginalValue * Amplitude * Phase;
  1099.     }
  1100.  
  1101.  
  1102. static float                        MultSignFuzz(LFOOneStateRec* State, float Phase,
  1103.                                                     float OriginalValue, float Amplitude)
  1104.     {
  1105.         long                                    OldSeed;
  1106.         float                                    ReturnValue;
  1107.  
  1108.         OldSeed = SetParkAndMillerRandomSeed(RandomSeed);
  1109.         ReturnValue = ((((float)ParkAndMillerRandom() - PARKANDMILLERMINIMUM)
  1110.             / (PARKANDMILLERMAXIMUM - PARKANDMILLERMINIMUM - 1) * 2) - 1) * Amplitude
  1111.             * OriginalValue;
  1112.         RandomSeed = SetParkAndMillerRandomSeed(OldSeed);
  1113.         return ReturnValue;
  1114.     }
  1115.  
  1116.  
  1117. static float                        MultPosFuzz(LFOOneStateRec* State, float Phase,
  1118.                                                     float OriginalValue, float Amplitude)
  1119.     {
  1120.         long                                    OldSeed;
  1121.         float                                    ReturnValue;
  1122.  
  1123.         OldSeed = SetParkAndMillerRandomSeed(RandomSeed);
  1124.         ReturnValue = (((float)ParkAndMillerRandom() - PARKANDMILLERMINIMUM)
  1125.             / (PARKANDMILLERMAXIMUM - PARKANDMILLERMINIMUM - 1) * Amplitude)
  1126.             * OriginalValue;
  1127.         RandomSeed = SetParkAndMillerRandomSeed(OldSeed);
  1128.         return ReturnValue;
  1129.     }
  1130.  
  1131.  
  1132. static float                        InvMultConst(LFOOneStateRec* State, float Phase,
  1133.                                                     float OriginalValue, float Amplitude)
  1134.     {
  1135.         return OriginalValue * (1 - Amplitude);
  1136.     }
  1137.  
  1138.  
  1139. static float                        InvMultSignSine(LFOOneStateRec* State, float Phase,
  1140.                                                     float OriginalValue, float Amplitude)
  1141.     {
  1142.         return OriginalValue * (1 - Amplitude * FSIN(Phase * TWOPI));
  1143.     }
  1144.  
  1145.  
  1146. static float                        InvMultPosSine(LFOOneStateRec* State, float Phase,
  1147.                                                     float OriginalValue, float Amplitude)
  1148.     {
  1149.         return OriginalValue * (1 - Amplitude * 0.5 * (1 + FSIN(Phase * TWOPI)));
  1150.     }
  1151.  
  1152.  
  1153. static float                        InvMultSignTriangle(LFOOneStateRec* State, float Phase,
  1154.                                                     float OriginalValue, float Amplitude)
  1155.     {
  1156.         if (Phase < 0.25)
  1157.             {
  1158.             }
  1159.         else if (Phase < 0.75)
  1160.             {
  1161.                 Phase = 0.5 - Phase;
  1162.             }
  1163.         else
  1164.             {
  1165.                 Phase = Phase - 1;
  1166.             }
  1167.         return OriginalValue * (1 - Phase * 4 * Amplitude);
  1168.     }
  1169.  
  1170.  
  1171. static float                        InvMultPosTriangle(LFOOneStateRec* State, float Phase,
  1172.                                                     float OriginalValue, float Amplitude)
  1173.     {
  1174.         if (Phase < 0.25)
  1175.             {
  1176.             }
  1177.         else if (Phase < 0.75)
  1178.             {
  1179.                 Phase = 0.5 - Phase;
  1180.             }
  1181.         else
  1182.             {
  1183.                 Phase = Phase - 1;
  1184.             }
  1185.         return OriginalValue * (1 - ((Phase + 1) * 2 * Amplitude));
  1186.     }
  1187.  
  1188.  
  1189. static float                        InvMultSignSquare(LFOOneStateRec* State, float Phase,
  1190.                                                     float OriginalValue, float Amplitude)
  1191.     {
  1192.         if (Phase < 0.5)
  1193.             {
  1194.                 return OriginalValue * (1 - Amplitude);
  1195.             }
  1196.          else
  1197.             {
  1198.                 return OriginalValue * (1 + Amplitude);
  1199.             }
  1200.     }
  1201.  
  1202.  
  1203. static float                        InvMultPosSquare(LFOOneStateRec* State, float Phase,
  1204.                                                     float OriginalValue, float Amplitude)
  1205.     {
  1206.         if (Phase < 0.5)
  1207.             {
  1208.                 return OriginalValue * (1 - Amplitude);
  1209.             }
  1210.          else
  1211.             {
  1212.                 return OriginalValue;
  1213.             }
  1214.     }
  1215.  
  1216.  
  1217. static float                        InvMultSignRamp(LFOOneStateRec* State, float Phase,
  1218.                                                     float OriginalValue, float Amplitude)
  1219.     {
  1220.         return OriginalValue * (1 - Amplitude * (2 * Phase - 1));
  1221.     }
  1222.  
  1223.  
  1224. static float                        InvMultPosRamp(LFOOneStateRec* State, float Phase,
  1225.                                                     float OriginalValue, float Amplitude)
  1226.     {
  1227.         return OriginalValue * (1 - Amplitude * Phase);
  1228.     }
  1229.  
  1230.  
  1231. static float                        InvMultSignFuzz(LFOOneStateRec* State, float Phase,
  1232.                                                     float OriginalValue, float Amplitude)
  1233.     {
  1234.         long                                    OldSeed;
  1235.         float                                    ReturnValue;
  1236.  
  1237.         OldSeed = SetParkAndMillerRandomSeed(RandomSeed);
  1238.         ReturnValue = (1 - ((((float)ParkAndMillerRandom() - PARKANDMILLERMINIMUM)
  1239.             / (PARKANDMILLERMAXIMUM - PARKANDMILLERMINIMUM - 1) * 2) - 1) * Amplitude)
  1240.             * OriginalValue;
  1241.         RandomSeed = SetParkAndMillerRandomSeed(OldSeed);
  1242.         return ReturnValue;
  1243.     }
  1244.  
  1245.  
  1246. static float                        InvMultPosFuzz(LFOOneStateRec* State, float Phase,
  1247.                                                     float OriginalValue, float Amplitude)
  1248.     {
  1249.         long                                    OldSeed;
  1250.         float                                    ReturnValue;
  1251.  
  1252.         OldSeed = SetParkAndMillerRandomSeed(RandomSeed);
  1253.         ReturnValue = (1 - (((float)ParkAndMillerRandom() - PARKANDMILLERMINIMUM)
  1254.             / (PARKANDMILLERMAXIMUM - PARKANDMILLERMINIMUM - 1) * Amplitude))
  1255.             * OriginalValue;
  1256.         RandomSeed = SetParkAndMillerRandomSeed(OldSeed);
  1257.         return ReturnValue;
  1258.     }
  1259.  
  1260.  
  1261. static FastFixedType        CalcWaveTableValue(LFOOneStateRec* State, float Phase)
  1262.     {
  1263.         FastFixedType                    Index;
  1264.         LongLongRec                        FrameIndex;
  1265.         signed long                        Final; /* both 16-bit int & largefixedsigned */
  1266.  
  1267.         ERROR(!State->WaveTableWasDefined,PRERR(ForceAbort,
  1268.             "CalcWaveTableValue:  no wave table"));
  1269.  
  1270.         Index = EnvelopeUpdate(State->WaveTableIndexEnvelope);
  1271.         if (Index < 0)
  1272.             {
  1273.                 Index = 0;
  1274.             }
  1275.         else if (Index > Int2FastFixed(State->NumberOfTables - 1))
  1276.             {
  1277.                 Index = Int2FastFixed(State->NumberOfTables - 1);
  1278.             }
  1279.         Double2LongLong(Phase,FrameIndex);
  1280.         if (State->TableNumBits == eSample8bit)
  1281.             {
  1282.                 /* 8-bit */
  1283.                 if (FastFixed2Int(Index) == State->NumberOfTables - 1)
  1284.                     {
  1285.                         /* this is done in case the wave table index is at the maximum, */
  1286.                         /* in which case there is no table+1 to interpolate with. */
  1287.                         signed char*                WaveData;
  1288.  
  1289.                         FastFixedType                LeftWeight;
  1290.                         long                                ArraySubscript;
  1291.                         signed long                    LeftValue;
  1292.                         signed long                    RightValue;
  1293.  
  1294.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1295.                             Index)]),sizeof(State->WaveTableMatrix[FastFixed2Int(Index)]));
  1296.                         WaveData = (signed char*)(State->WaveTableMatrix[FastFixed2Int(Index)]);
  1297.  
  1298.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  1299.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  1300.                         /* L+F(R-L) */
  1301.                         LeftValue = ((signed long)WaveData[ArraySubscript]) << 8; /* convert to 16-bit */
  1302.                         RightValue = ((signed long)WaveData[ArraySubscript + 1]) << 8; /* to 16-bit */
  1303.                         Final = LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  1304.                     }
  1305.                  else
  1306.                     {
  1307.                         signed char*                WaveData0;
  1308.                         signed char*                WaveData1;
  1309.                         FastFixedType                Wave0Weight;
  1310.  
  1311.                         FastFixedType                LeftWeight;
  1312.                         long                                ArraySubscript;
  1313.                         signed long                    Left0Value;
  1314.                         signed long                    Right0Value;
  1315.                         signed long                    Left1Value;
  1316.                         signed long                    Right1Value;
  1317.                         FastFixedType                Wave0Temp;
  1318.  
  1319.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1320.                             Index)]),sizeof(State->WaveTableMatrix[FastFixed2Int(Index)]));
  1321.                         WaveData0 = (signed char*)(State->WaveTableMatrix[
  1322.                             FastFixed2Int(Index)]);
  1323.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1324.                             Index) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(Index) + 1]));
  1325.                         WaveData1 = (signed char*)(State->WaveTableMatrix[
  1326.                             FastFixed2Int(Index) + 1]);
  1327.                         Wave0Weight = Index & FASTFIXEDFRACTMASK;
  1328.  
  1329.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  1330.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  1331.                         /* L+F(R-L) -- applied twice */
  1332.                         Left0Value = ((signed long)WaveData0[ArraySubscript]) << 8; /* convert to 16-bit */
  1333.                         Right0Value = ((signed long)WaveData0[ArraySubscript + 1]) << 8; /* to 16-bit */
  1334.                         Left1Value = ((signed long)WaveData1[ArraySubscript]) << 8; /* convert to 16-bit */
  1335.                         Right1Value = ((signed long)WaveData1[ArraySubscript + 1]) << 8; /* to 16-bit */
  1336.                         Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  1337.                         Final = Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight
  1338.                             * (Right1Value - Left1Value)) >> 15) - Wave0Temp)) >> 15);
  1339.                     }
  1340.             }
  1341.          else
  1342.             {
  1343.                 /* 16-bit */
  1344.                 if (FastFixed2Int(Index) == State->NumberOfTables - 1)
  1345.                     {
  1346.                         /* this is done in case the wave table index is at the maximum, */
  1347.                         /* in which case there is no table+1 to interpolate with. */
  1348.                         signed short*                WaveData;
  1349.  
  1350.                         FastFixedType                LeftWeight;
  1351.                         long                                ArraySubscript;
  1352.                         signed long                    LeftValue;
  1353.                         signed long                    RightValue;
  1354.  
  1355.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1356.                             Index)]),sizeof(State->WaveTableMatrix[FastFixed2Int(Index)]));
  1357.                         WaveData = (signed short*)(State->WaveTableMatrix[FastFixed2Int(Index)]);
  1358.  
  1359.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  1360.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  1361.                         /* L+F(R-L) */
  1362.                         LeftValue = WaveData[ArraySubscript];
  1363.                         RightValue = WaveData[ArraySubscript + 1];
  1364.                         Final = LeftValue + ((LeftWeight * (RightValue - LeftValue)) >> 15);
  1365.                     }
  1366.                  else
  1367.                     {
  1368.                         signed short*                WaveData0;
  1369.                         signed short*                WaveData1;
  1370.                         FastFixedType                Wave0Weight;
  1371.  
  1372.                         FastFixedType                LeftWeight;
  1373.                         long                                ArraySubscript;
  1374.                         signed long                    Left0Value;
  1375.                         signed long                    Right0Value;
  1376.                         signed long                    Left1Value;
  1377.                         signed long                    Right1Value;
  1378.                         FastFixedType                Wave0Temp;
  1379.  
  1380.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1381.                             Index)]),sizeof(State->WaveTableMatrix[FastFixed2Int(Index)]));
  1382.                         WaveData0 = (signed short*)(State->WaveTableMatrix[
  1383.                             FastFixed2Int(Index)]);
  1384.                         PRNGCHK(State->WaveTableMatrix,&(State->WaveTableMatrix[FastFixed2Int(
  1385.                             Index) + 1]),sizeof(State->WaveTableMatrix[FastFixed2Int(Index) + 1]));
  1386.                         WaveData1 = (signed short*)(State->WaveTableMatrix[
  1387.                             FastFixed2Int(Index) + 1]);
  1388.                         Wave0Weight = Index & FASTFIXEDFRACTMASK;
  1389.  
  1390.                         LeftWeight = LongLongLowHalf(FrameIndex) >> (32 - FASTFIXEDPRECISION);
  1391.                         ArraySubscript = LongLongHighHalf(FrameIndex) & (State->FramesPerTable - 1);
  1392.                         /* L+F(R-L) -- applied twice */
  1393.                         Left0Value = WaveData0[ArraySubscript];
  1394.                         Right0Value = WaveData0[ArraySubscript + 1];
  1395.                         Left1Value = WaveData1[ArraySubscript];
  1396.                         Right1Value = WaveData1[ArraySubscript + 1];
  1397.                         Wave0Temp = Left0Value + ((LeftWeight * (Right0Value - Left0Value)) >> 15);
  1398.                         Final = Wave0Temp + ((Wave0Weight * (Left1Value + ((LeftWeight
  1399.                             * (Right1Value - Left1Value)) >> 15)) - Wave0Temp) >> 15);
  1400.                     }
  1401.             }
  1402.         return Final;
  1403.     }
  1404.  
  1405.  
  1406. static float                        AddWaveTable(LFOOneStateRec* State, float Phase,
  1407.                                                     float OriginalValue, float Amplitude)
  1408.     {
  1409.         if (State->WaveTableWasDefined)
  1410.             {
  1411.                 return OriginalValue + Amplitude
  1412.                     * ((float)CalcWaveTableValue(State,Phase) / MAX16BIT);
  1413.             }
  1414.          else
  1415.             {
  1416.                 return OriginalValue;
  1417.             }
  1418.     }
  1419.  
  1420.  
  1421. static float                        MultWaveTable(LFOOneStateRec* State, float Phase,
  1422.                                                     float OriginalValue, float Amplitude)
  1423.     {
  1424.         if (State->WaveTableWasDefined)
  1425.             {
  1426.                 return OriginalValue * Amplitude
  1427.                     * ((float)CalcWaveTableValue(State,Phase) / MAX16BIT);
  1428.             }
  1429.          else
  1430.             {
  1431.                 return 0;
  1432.             }
  1433.     }
  1434.  
  1435.  
  1436. static float                        InvMultWaveTable(LFOOneStateRec* State, float Phase,
  1437.                                                     float OriginalValue, float Amplitude)
  1438.     {
  1439.         if (State->WaveTableWasDefined)
  1440.             {
  1441.                 return OriginalValue * (1 - Amplitude
  1442.                     * ((float)CalcWaveTableValue(State,Phase) / MAX16BIT));
  1443.             }
  1444.          else
  1445.             {
  1446.                 return OriginalValue;
  1447.             }
  1448.     }
  1449.